<template>
  <div id="video" v-if="isShow">
    <div class="close" @click="closePane">×</div>
    <div class="title">{{name}}</div>
    <div class="content">
      <video
        muted
          controls
          class="video"
          ref="video"
          style="width: 100%; height: 100%;"
        ></video>
    </div>
  </div>
</template>
<script>
import axios from "axios";
import { videoUrl } from "../../utils/api"
let Hls = require("hls.js");
export default {
  data() {
    return {
      name:"---",
      isShow: false,
      hls: null,
    };
  },
  mounted() {
    window.showvideo = this.video;
  },
  methods: {
    // 接收传送过来的信息
    video(val) {
      this.isShow = val.show;
      this.name = val.title;
      if(val.ele==undefined||val.ele=="undefined"){
        this.$message("无视频地址");
        this.closePane();
      }else{
        // let param ={ cameraIndexCode:val.ele, protocol: "hls", isTest:"0" };// 外网1，内网0
        var param = '?cameraIndexCode='+val.ele+'&protocol=hls&isTest=0'
        videoUrl(param).then((res)=>{
          this.getStream(res&&res.data&&res.data.data&&res.data.data.url)
        })
      }
    },
    // 关闭弹窗
    closePane() {
      this.isShow = false;
      this.videoPause();
    },
    getStream(source) {
      if (Hls.isSupported()) {
        this.hls = new Hls();
        this.hls.loadSource(source);
        this.hls.attachMedia(this.$refs.video);

        this.hls.on(Hls.Events.MANIFEST_PARSED, () => {
          console.log("视频加载成功");
          this.$refs.video.play();
        });
        this.hls.on(Hls.Events.ERROR, (event, data) => {
          console.log("视频加载失败");
        });
      } else if (
        this.$refs.video.canPlayType("application/vnd.apple.mpegurl")
      ) {
        this.$refs.video.src =
          "https://video-dev.github.io/streams/x36xhzz/x36xhzz.m3u8";
        this.$refs.video.addEventListener("loadedmetadata", function () {
          this.$refs.video.play();
        });
      }
    },
    // 停止流
    videoPause() {
      if (this.hls) {
        this.$refs.video.pause();
        this.hls.destroy();
        this.hls = null;
      }
    },
  },
};
</script>
<style scoped>
#video {
  margin: 0 auto;
  box-sizing: border-box;
  position: absolute;
  left: 36%;
  top: 520px;
  width: 1190px;
  height: 800px;
  color: white;
  background: url("../../../static/images/popups/detailsPane.png") !important;
  background-position: center !important;
  background-repeat: no-repeat !important;
  background-size: 100% 100% !important;
  transition: all 1.2s;
}
/* 右上角关闭按钮 */
.close {
  float: right;
  font-size: 50px;
  font-weight: 900;
  margin-right: 30px;
  margin-top: 10px;
  transition: all 1s;
}
.close:hover {
  transform: scale(1.1);
  color: red;
  cursor: pointer;
}
.content {
  box-sizing: border-box;
  width: 100%;
  height: 800px;
  padding: 80px;
  margin-top: 30px;
}
.title {
  position: absolute;
  left: 40px;
  top: 30px;
  font-size: 32px;
  font-weight: 800;
}
</style>